﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ServerManager.Data;
using ServerManager.Models;
using ServerManager.Repository;

namespace ServerManager.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Authorize]
    [Route("[controller]")]
    public class ServerController : Controller
    {
        private readonly IServerRepository _serverRepository;

        public ServerController(IServerRepository serverRepository)
        {
            _serverRepository = serverRepository;
        }

        public IActionResult Index()
        {
            //var list = _serverRepository.TableNoTracking.OrderBy(x => x.Position);
            //return View(list);

            var list = _serverRepository.GetDbContext().Servers.Include(x => x.Apps).Include(x => x.IpAddresses).OrderBy(x => x.Position);
            return View(list);
        }

        [HttpGet("[action]/{id?}")]
        public IActionResult CreateOrUpdate(int? id)
        {
            if(id == null)
            {
                BuildStatusList(2);
                return View();
            }
            else
            {
                var s = _serverRepository.Find(x => x.Id == id);
                BuildStatusList(s.Status);
                return View(s);
            }
        }

        [HttpPost("[action]/{id?}")]
        public async Task<IActionResult> CreateOrUpdate(int? id, Server s)
        {
            if(ModelState.IsValid)
            {
                if(id == null)
                {
                    s.CreateTime = DateTime.Now;
                    await _serverRepository.AddAsync(s);
                }
                else
                {
                    if(id == s.Id)
                    {
                        await _serverRepository.UpdateAsync(s);
                    }
                }
            }
            return RedirectToAction(nameof(Index));
        }

        [HttpGet("[action]/{id}")]
        public async Task<IActionResult> Details(int id)
        {
            var s = await _serverRepository.GetById(id);
            return View(s);
        }

        [HttpGet("[action]/{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            await _serverRepository.DeleteAsync(x => x.Id == id);
            return RedirectToAction(nameof(Index));
        }

        private void BuildStatusList(int selected)
        {
            bool s0 = false;
            bool s1 = false;
            bool s2 = false;

            if (selected == 0)
                s0 = true;
            else if (selected == 1)
                s1 = true;
            else if (selected == 2)
                s2 = true;

            var list = new List<SelectListItem>
            {
                 new SelectListItem { Text = "不可用", Value = "0", Selected = s0 },
                 new SelectListItem { Text = "空闲", Value = "1", Selected = s1 },
                 new SelectListItem { Text = "占用", Value = "2", Selected = s2 }
            };

            ViewBag.StatusList = list;
        }
    }
}